﻿using QuanLyThuVien.DAL;
using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThuVien.BLL
{
    //----------------------------------------------------------------------
    //Desc: class xử lý cho bảng SACH
    //----------------------------------------------------------------------
    public class BLLSach
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public BLLSach()
        { }

        //----------------------------------------------------------------------
        //Desc: lấy sách
        //----------------------------------------------------------------------
        public DTOSach LaySach(string masach)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.LaySach(masach);
        }

        //----------------------------------------------------------------------
        //Desc: lấy mã sách
        //----------------------------------------------------------------------
        public string LayMaSach(string madausach, long namxb, string nhaxb)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.LayMaSach(madausach, namxb, nhaxb);
        }

        //----------------------------------------------------------------------
        //Desc: phát sinh mã sách
        //----------------------------------------------------------------------
        public bool PhatSinhMaSach(ref DTOSach sach)
        {
            DALSach _dalSach = new DALSach();
            int stt = _dalSach.DemSach(sach.MaDauSach);
            if (stt < 0)
                return false;
            if (stt == 0)
                stt++;
            string masach = sach.MaDauSach + stt.ToString("D3");
            int dem = _dalSach.DemMaSach(masach);
            while (dem > 0)
            {
                stt++;
                masach = sach.MaDauSach + stt.ToString("D3");
                dem = _dalSach.DemMaSach(masach);
            }

            if (dem < 0)
                return false;

            sach.MaSach = masach;
            return true;
        }

        //----------------------------------------------------------------------
        //Desc: lấy số lượng sách
        //----------------------------------------------------------------------
        public int LaySoLuongSach(string masach)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.LaySoLuongSach(masach);
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật số lượng
        //----------------------------------------------------------------------
        public bool CapNhatSoLuong(string masach, int soluongmoi)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.CapNhatSoLuong(masach, soluongmoi);
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật sách
        //----------------------------------------------------------------------
        public bool CapNhat(DTOSach sach)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.CapNhat(sach);
        }


        //----------------------------------------------------------------------
        //Desc: lấy danh sách thông tin sách
        //----------------------------------------------------------------------
        public DataTable LayDSThongTinSach()
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.LayDSThongTinSach();
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách thông tin sách mượn
        //----------------------------------------------------------------------
        public DataTable LayDSThongTinSachMuon()
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.LayDSThongTinSachMuon();
        }

        //----------------------------------------------------------------------
        //Desc: đếm lần xuất bản
        //----------------------------------------------------------------------
        public int DemLanXuatBan(string masach, long namxb, string nhaxb)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.DemLanXuatBan(masach, namxb, nhaxb);
        }

        //----------------------------------------------------------------------
        //Desc: tra cứu sách
        //----------------------------------------------------------------------
        public DataTable TraCuuSach(string masach, string tendausach, string matl, string matg,
            long namxb, string nhaxb, object NgayNhap, double trigiamin, double trigiamax)
        {
            DALSach _dalSach = new DALSach();
            return _dalSach.TraCuuSach(masach, tendausach, matl, matg, namxb, nhaxb, NgayNhap, trigiamin, trigiamax);
        }
    }
}
